/**
 * Copyright (C), 2020, Y2 T205
 * FileName: OAuth2Controller
 * Author: Lenovo
 * Date: 2020-05-26 9:47
 * Description:
 * History:
 * <author> <time> <version> <desc>
 * 作者姓名 修改时间 版本号 描述
 */


package com.atguigu.gulimall.gulimallauth.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.atguigu.common.utils.HttpUtils;
import com.atguigu.common.utils.R;

import com.atguigu.gulimall.gulimallauth.feign.MemberFeingService;
import com.atguigu.common.vo.MemberRespVo;
import com.atguigu.gulimall.gulimallauth.vo.SocialUser;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * 〈一句话功能简述〉<br>
 * 〈〉
 *
 * @author Lenovo
 * @create 2020-05-26
 * @since 1.0.0
 */
//处理社交登陆
    @Slf4j
    @Controller
public class OAuth2Controller {

        @Autowired
        MemberFeingService memberFeingService;

        @GetMapping("/oauth2.0/weibo/success")
    public String weibo(@RequestParam("code") String code, HttpSession session) throws Exception {
            Map<String,String> header =new HashMap<>();
            Map<String,String> query =new HashMap<>();

            Map<String,String> map =new HashMap<>();
            map.put("client_id","1798874871");
            map.put("client_secret","8b0a03365771b6611f128139ea5d18b9");
            map.put("grant_type","authorization_code");
            map.put("redirect_uri","http://auth.gulimall.com/oauth2.0/weibo/success");
            map.put("code",code);

//            1.根据code换取accessToken

            HttpResponse response = HttpUtils.doPost("https://api.weibo.com", "/oauth2/access_token", "post", header, query, map);

//            2.处理
           if(response.getStatusLine().getStatusCode()==200){
//               获取到啦accessToken
               String json = EntityUtils.toString(response.getEntity());
               SocialUser socialUser = JSON.parseObject(json, SocialUser.class);

//               知道当前的是哪个社交用户
//               1).当前用户如果是第一次注册
               R oauthlogin = memberFeingService.oauthlogin(socialUser);
               if(oauthlogin.getCode()==0){
                   MemberRespVo data = oauthlogin.getData("data", new TypeReference<MemberRespVo>() {
                   });
                   //System.out.println("登陆成功"+data);
                   log.info("登陆成功：用户：{}",data.toString());
                   //            2.登陆成功跳回首页
                   session.setAttribute("loginUser",data);

                   return "redirect:http://gulimall.com";
               }else{
                   return "redirect:http://auth.gulimall.com/login.html";
               }

//               2）不是第一次登陆
           }else{
               return "redirect:http://auth.gulimall.com/login.html";
           }

        }

}
